package com.ruoyi.bantong.service.impl;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.bantong.constant.BanTongConstants;
import com.ruoyi.bantong.domain.BtOrganization;
import com.ruoyi.bantong.domain.BtOrganizationAccountRecord;
import com.ruoyi.bantong.mapper.BtOrganizationMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bantong.mapper.BtOrganizationRechargeRecordMapper;
import com.ruoyi.bantong.domain.BtOrganizationRechargeRecord;
import com.ruoyi.bantong.service.IBtOrganizationRechargeRecordService;

/**
 * 机构充值记录Service业务层处理
 * 
 * @author laohei
 * @date 2021-02-05
 */
@Service
public class BtOrganizationRechargeRecordServiceImpl implements IBtOrganizationRechargeRecordService 
{
    @Autowired
    private BtOrganizationRechargeRecordMapper btOrganizationRechargeRecordMapper;
    @Autowired
    private BtOrganizationMapper organizationMapper;

    /**
     * 查询机构充值记录
     * 
     * @param orrId 机构充值记录ID
     * @return 机构充值记录
     */
    @Override
    public BtOrganizationRechargeRecord selectBtOrganizationRechargeRecordById(Integer orrId)
    {
        return btOrganizationRechargeRecordMapper.selectBtOrganizationRechargeRecordById(orrId);
    }

    /**
     * 查询机构充值记录列表
     * 
     * @param btOrganizationRechargeRecord 机构充值记录
     * @return 机构充值记录
     */
    @Override
    public List<BtOrganizationRechargeRecord> selectBtOrganizationRechargeRecordList(BtOrganizationRechargeRecord btOrganizationRechargeRecord)
    {
        return btOrganizationRechargeRecordMapper.selectBtOrganizationRechargeRecordList(btOrganizationRechargeRecord);
    }

    /**
     * 新增机构充值记录
     * 
     * @param btOrganizationRechargeRecord 机构充值记录
     * @return 结果
     */
    @Override
    public int insertBtOrganizationRechargeRecord(BtOrganizationRechargeRecord btOrganizationRechargeRecord)
    {
        btOrganizationRechargeRecord.setCreateTime(DateUtils.getNowDate());
        return btOrganizationRechargeRecordMapper.insertBtOrganizationRechargeRecord(btOrganizationRechargeRecord);
    }

    /**
     * 修改机构充值记录
     * 
     * @param btOrganizationRechargeRecord 机构充值记录
     * @return 结果
     */
    @Override
    public int updateBtOrganizationRechargeRecord(BtOrganizationRechargeRecord btOrganizationRechargeRecord)
    {
        btOrganizationRechargeRecord.setUpdateTime(DateUtils.getNowDate());
        return btOrganizationRechargeRecordMapper.updateBtOrganizationRechargeRecord(btOrganizationRechargeRecord);
    }

    /**
     * 批量删除机构充值记录
     * 
     * @param orrIds 需要删除的机构充值记录ID
     * @return 结果
     */
    @Override
    public int deleteBtOrganizationRechargeRecordByIds(Integer[] orrIds)
    {
        return btOrganizationRechargeRecordMapper.deleteBtOrganizationRechargeRecordByIds(orrIds);
    }

    /**
     * 删除机构充值记录信息
     * 
     * @param orrId 机构充值记录ID
     * @return 结果
     */
    @Override
    public int deleteBtOrganizationRechargeRecordById(Integer orrId)
    {
        return btOrganizationRechargeRecordMapper.deleteBtOrganizationRechargeRecordById(orrId);
    }

    @Override
    public void updataOrgRecharge(Integer orgId,Integer userId, BigDecimal bean, BigDecimal score, String changeType, Integer actionType) throws Exception {
        BtOrganization btOrganization = organizationMapper.selectBtOrganizationById(orgId);

        BtOrganizationRechargeRecord accountRecord = new BtOrganizationRechargeRecord();
        accountRecord.setOrgId(orgId);
        accountRecord.setUserId(userId);
        accountRecord.setBefBeanBalance(btOrganization.getReBean());
        accountRecord.setBean(bean);
        BigDecimal BigBtBean = btOrganization.getReBean();
        BigDecimal Bigbean = bean;
        BigDecimal BigBtBeanBalance = BigDecimal.ZERO;

        if("in".equals(changeType)){
            BigBtBeanBalance = BigBtBean.add(Bigbean);

        }else {
            BigBtBeanBalance = BigBtBean.subtract(Bigbean);
        }
        accountRecord.setBeanBalance(BigBtBeanBalance);
        accountRecord.setActionType((Integer)getFinalFieldValueByName("ORG_RECHARGE_ACTION_TYPE" + actionType, BanTongConstants.class));//参加席位玩法  扣除保证金
        accountRecord.setMemo((String)getFinalFieldValueByName("ORG_RECHARGE_MEMO_TYPE" + actionType, BanTongConstants.class));
        accountRecord.setChangeType(changeType);
        accountRecord.setCreateTime(DateUtils.getNowDate());
        String createBy = SecurityUtils.getUsername();
        accountRecord.setCreateBy(StringUtils.isEmpty(createBy) ? null : createBy);
        btOrganizationRechargeRecordMapper.insertBtOrganizationRechargeRecord(accountRecord);

        //用户账户变动
        BtOrganization Organization1 = new BtOrganization();
        Organization1.setOrgId(orgId);
        Organization1.setReBean(BigBtBeanBalance);
        Organization1.setUpdateTime(DateUtils.getNowDate());
        organizationMapper.updateBtOrganization(Organization1);
    }

    /**
     * 通过常量属性名字,在字节码中获取到一个常量的值
     * 这个方法常用于取出接口中常量的值,可以适用于接口中的常量;类中的常量,但是仅限于常量.如果是变量,返回为空
     * 注意: 如果你要取出来的不是常量,我也给你返回了null,这是为了确保,取出来的一定是 final 的常量
     * @param   fieldName  属性的名字
     * @param   clazz   在那个字节码中
     * @return  如果找找到,并且他也是常量,我就返回它的值,否则返回null
     * @throws Exception 反射的风险异常.
     */
    public static Object getFinalFieldValueByName(String fieldName, Class<?> clazz) throws Exception {
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            //确保你要取的,是常量.否则,就不用进去这个获取操作了
            boolean isFinal = Modifier.isFinal(field.getModifiers());
            if(isFinal){
                String name = field.getName();
                if (fieldName.equals(name)) {
                    return field.get(null);
                }
            }
        }
        return null;
    }
}
